package com.yonyou.pmclouds.safetyrectification.process;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogDetailVo;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogJobVo;
import com.yonyou.pmclouds.projectlog.entity.SafetyRectifyContentVO;
import com.yonyou.pmclouds.projectlog.rmiitf.ProjectLogMaintain;
import com.yonyou.pmclouds.reason.entity.ProblemReasonVO;
import com.yonyou.pmclouds.reason.rmiitf.ProblemReasonQuery;
import com.yonyou.pmclouds.safetyrectification.entity.SafetyRectificationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * 智能日志维护安全整改后置处理器
 *
 * @author th
 * @date 2019/10/30 10:51
 */
@Slf4j
@Component
public class SafetyRectifyProjectLogPostProcess implements SingleProcessor<SafetyRectificationVO> {

    private static ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private ProjectLogMaintain projectLogMaintain;
    @Autowired
    private ProblemReasonQuery problemReasonQuery;

    @Override
    public void process(SafetyRectificationVO vo, SafetyRectificationVO orig) throws BusinessException {
        // 新增和交办触发
        if (SafetyRectificationVO.OPERATION_ADD.equals(vo.getOperation()) ||
                SafetyRectificationVO.OPERATION_ASSIGN.equals(vo.getOperation())) {
            try {
                //最终放入队列的任务对象
                ProjectLogJobVo jobVo = new ProjectLogJobVo();

                jobVo.setPkTenant(vo.getPkTenant());
                jobVo.setPkProject(vo.getPkProject());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                jobVo.setLogDate(calendar.getTime());
                jobVo.setEventType(ProjectLogJobVo.EVENT_TYPE_DETAIL);
                jobVo.setEventTime(vo.getCreationtime());
                jobVo.setUserId(vo.getCreator());

                // 生成项目日志子表
                ProjectLogDetailVo detailVo = generateDetailVO(vo);
                jobVo.setAttachment(detailVo);

                projectLogMaintain.upsert(jobVo);
            } catch (Exception e) {
                log.error("安全整改单--智能日志处理出错，安全整改pk：{}", vo.getPkSafetyRectification());
                log.error("异常信息", e);
            }
        }
    }

    /**
     * 生成项目日志详情VO
     *
     * @param vo
     * @return
     */
    private ProjectLogDetailVo generateDetailVO(SafetyRectificationVO vo) throws BusinessException, JsonProcessingException {
        ProjectLogDetailVo logDetail = new ProjectLogDetailVo();
        logDetail.setDetailType(ProjectLogDetailVo.SAFETY_RECTIFY);
        logDetail.setPkContentSource(vo.getPkSafetyRectification());
        logDetail.setCreator(vo.getCreator());
        logDetail.setCreationtime(vo.getCreationtime());
        // 整改，记录修改人、修改时间
        if (SafetyRectificationVO.OPERATION_RECTIFY.equals(vo.getOperation())) {
            logDetail.setModifier(vo.getModifier());
            logDetail.setModifytime(vo.getModifytime());
        }
        SafetyRectifyContentVO contentVO = generateContentVO(vo);
        logDetail.setContentJson(objectMapper.writeValueAsString(contentVO));

        return logDetail;
    }

    /**
     * 生成界面显示的内容VO
     *
     * @param vo
     * @return
     */
    private SafetyRectifyContentVO generateContentVO(SafetyRectificationVO vo) throws BusinessException {
        SafetyRectifyContentVO contentVO = new SafetyRectifyContentVO();
        contentVO.setPkBill(vo.getPkSafetyRectification());
        contentVO.setBillType(BillTypeConst.SAFETY_RECTIFICATION_BILLTYPE);
        contentVO.setCreatorName(vo.getCreatorName());
        // 查询问题原因名称
        ProblemReasonVO reasonVO = problemReasonQuery.getById(vo.getPkProblemReason());
        if (reasonVO != null) {
            vo.setProblemReasonName(reasonVO.getSname());
        }
        contentVO.setProblemReasonName(vo.getProblemReasonName());
        contentVO.setRectificationArea(vo.getRectificationArea());
        contentVO.setRectificationerName(vo.getRectificationerName());
        contentVO.setSeverityLevel(vo.getSeverityLevel());

        return contentVO;
    }
}
